package com.vhukze.basics.aspect;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.vhukze.basics.util.MyUtils;
import lombok.extern.log4j.Log4j2;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 业务层方法打印调用日志
 *
 * @author vhukze
 * @date 2021/12/2 15:35
 */
@Log4j2
@Aspect
@Component
@Order(1)
public class ServiceLogAOP {

    @Pointcut("@within(org.springframework.stereotype.Service)")
    private void serviceLogPointcut() {
    }

    @Before("serviceLogPointcut()")
    public void serviceLog(JoinPoint joinPoint) {

        String methodName = joinPoint.getSignature().getName();

        log.info("调用方法名称：" + methodName);

        Map<String, Object> nameAndValue = MyUtils.aopGetNameAndValue(joinPoint);

        JSONObject paramsJson = JSONUtil.parseObj(nameAndValue);
        log.info("方法" + methodName + ">>>参数列表：" + paramsJson);

    }


}
